package com.ruoyi.project.kkad.advertising.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.kkad.adplace.domain.AdvertisingPlace;
import com.ruoyi.project.kkad.adplace.service.IAdvertisingPlaceService;
import com.ruoyi.project.kkad.advertising.domain.Advertising;
import com.ruoyi.project.kkad.advertising.service.IAdvertisingService;

/**
 * 广告管理Controller
 * 
 * @author shixiangwen
 * @date 2020-04-13
 */
@Controller
@RequestMapping("/kkad/advertising")
public class AdvertisingController extends BaseController {
	private String prefix = "kkad/advertising";

	@Autowired
	IAdvertisingService advertisingService;

	@Autowired
	IAdvertisingPlaceService advertisingPlaceService;

	@RequiresPermissions("kkad:advertising:view")
	@GetMapping("/{platform}")
	public String advertising(@PathVariable("platform") String platform, ModelMap mmap) {
		mmap.addAttribute("platform", platform);
		return prefix + "/advertising";
	}

	/**
	 * 查询广告管理列表
	 */
	@RequiresPermissions("kkad:advertising:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Advertising advertising) {
		startPage();
		List<Advertising> list = advertisingService.selectAdvertisingList(advertising);
		return getDataTable(list);
	}

	/**
	 * 导出广告管理列表
	 */
	@RequiresPermissions("kkad:advertising:export")
	@PostMapping("/export")
	@ResponseBody
	public AjaxResult export(Advertising advertising) {
		List<Advertising> list = advertisingService.selectAdvertisingList(advertising);
		ExcelUtil<Advertising> util = new ExcelUtil<Advertising>(Advertising.class);
		return util.exportExcel(list, "advertising");
	}

	/**
	 * 新增广告管理
	 */
	@GetMapping("/add/{param}")
	public String add(@PathVariable("param") String param, ModelMap mmap) {
		String[] datas = param.split(":");

		String appId = datas[0];
		String platform = datas[1];

		mmap.addAttribute("appId", appId);
		mmap.addAttribute("platform", platform);

		AdvertisingPlace query = new AdvertisingPlace();
		query.setPlatform(platform);
		query.setAppId(appId);
		List<AdvertisingPlace> placeList = advertisingPlaceService.selectAdvertisingPlaceList(query);

		mmap.put("placeList", placeList);

		return prefix + "/add";
	}

	/**
	 * 新增保存广告管理
	 */
	@RequiresPermissions("kkad:advertising:add")
	@Log(title = "广告管理", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(Advertising advertising) {
		advertising.setCreatedUserId(ShiroUtils.getLoginName());
		advertising.setCreatedTime(DateUtils.getNowDate());
		return toAjax(advertisingService.insertAdvertising(advertising));
	}

	/**
	 * 修改广告管理
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap) {
		Advertising advertising = advertisingService.selectAdvertisingById(id);

		mmap.put("advertising", advertising);

		AdvertisingPlace query = new AdvertisingPlace();
		query.setPlatform(advertising.getPlatform());
		query.setAppId(advertising.getAppId());
		List<AdvertisingPlace> placeList = advertisingPlaceService.selectAdvertisingPlaceList(query);

		mmap.put("placeList", placeList);

		return prefix + "/edit";
	}

	/**
	 * 修改保存广告管理
	 */
	@RequiresPermissions("kkad:advertising:edit")
	@Log(title = "广告管理", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(Advertising advertising) {
		return toAjax(advertisingService.updateAdvertising(advertising));
	}

	/**
	 * 删除广告管理
	 */
	@RequiresPermissions("kkad:advertising:remove")
	@Log(title = "广告管理", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return toAjax(advertisingService.deleteAdvertisingByIds(ids));
	}
}
